﻿using System;
using System.Collections.Generic;
using System.IO.Compression;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace LX.MVC.Open.CustomExtend
{
    public class CompressActionFilterAttribute: ActionFilterAttribute
    {

        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            string acceptEncoding = filterContext.HttpContext.Request.Headers["accept-encoding"];
            if (string.IsNullOrEmpty(acceptEncoding))
            {
                return;
            }
            else
            {
                if (acceptEncoding.ToLower().Contains("gzip"))
                {
                    var response = filterContext.HttpContext.Response;
                    response.AddHeader("content-encoding", "gzip");
                    response.Filter = new GZipStream(response.Filter, CompressionMode.Compress);


                }
            }

        }

    }
}